package activity.live;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import com.hichip.campro.R;

import utils.HiToast;

//直播界面预置位的model  懒汉模式
public class LiveViewPresetModel implements View.OnClickListener, TextWatcher {
    private static  LiveViewPresetModel instance=null;
    private static  final Object LOCK=new Object();
    private TextView tv_key_preset;//显示预置位数字
    private String roomInput;
    private Context mContext;


    private LiveViewPresetModel() {

    }

    public static LiveViewPresetModel  getInstance(){
        if(instance==null){
            synchronized (LOCK){
                instance=new LiveViewPresetModel();
            }
        }
        return instance;
    }

    public void handKeyBoard(Context context,View view){
        mContext=context;
        TextView tv_one=view.findViewById(R.id.tv_one);
        tv_one.setOnClickListener(this);
        TextView tv_two=view.findViewById(R.id.tv_two);
        tv_two.setOnClickListener(this);
        TextView tv_there=view.findViewById(R.id.tv_there);
        tv_there.setOnClickListener(this);
        TextView tv_four=view.findViewById(R.id.tv_four);
        tv_four.setOnClickListener(this);

        TextView tv_five=view.findViewById(R.id.tv_five);
        tv_five.setOnClickListener(this);
        TextView tv_six=view.findViewById(R.id.tv_six);
        tv_six.setOnClickListener(this);
        TextView tv_seven=view.findViewById(R.id.tv_seven);
        tv_seven.setOnClickListener(this);
        TextView tv_delete=view.findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(this);

        TextView tv_eight=view.findViewById(R.id.tv_eight);
        tv_eight.setOnClickListener(this);
        TextView tv_nine=view.findViewById(R.id.tv_nine);
        tv_nine.setOnClickListener(this);
        TextView tv_zero=view.findViewById(R.id.tv_zero);
        tv_zero.setOnClickListener(this);
        TextView tv_confirm=view.findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(this);

        tv_key_preset=view.findViewById(R.id.tv_key_preset);
        tv_key_preset.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        roomInput=tv_key_preset.getText().toString().trim();
        switch (v.getId()){
            case R.id.tv_one:
                tv_key_preset.setText(roomInput+"1");
                break;
            case R.id.tv_two:
                tv_key_preset.setText(roomInput+"2");
                break;
            case R.id.tv_there:
                tv_key_preset.setText(roomInput+"3");
                break;
            case R.id.tv_four:
                tv_key_preset.setText(roomInput+"4");
                break;
            case R.id.tv_five:
                tv_key_preset.setText(roomInput+"5");
                break;
            case R.id.tv_six:
                tv_key_preset.setText(roomInput+"6");
                break;
            case R.id.tv_seven:
                tv_key_preset.setText(roomInput+"7");
                break;
            case R.id.tv_delete://删除
                tv_key_preset.setText("");
                break;
            case R.id.tv_eight:
                tv_key_preset.setText(roomInput+"8");
                break;
            case R.id.tv_nine:
                tv_key_preset.setText(roomInput+"9");
                break;
            case R.id.tv_zero:
                tv_key_preset.setText(roomInput+"0");
                break;
            case R.id.tv_confirm://确认
                if(onClickConfirm!=null){
                    String strPreset=tv_key_preset.getText().toString();
                    onClickConfirm.onConfirm(strPreset);
                }
                break;

        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(s.toString().trim())) {
            Integer num = Integer.parseInt(s.toString());
            if (num == 0 || num > 255) {
                HiToast.showToast(mContext, mContext.getString(R.string.tip_perset_toast));
            }
        }
    }

    public void setOnClickConfirm(OnClickConfirm onClickConfirm){
        if(onClickConfirm!=null){
            this.onClickConfirm= onClickConfirm;
        }
    }
    public interface OnClickConfirm{
        void onConfirm(String strPreset);
    }
    public OnClickConfirm onClickConfirm;
}



